﻿using System;
using System.Xml.Linq;
using BMServiceGenerator.Tests.ApprovedInvoicesService;
using BMServiceGenerator.Tests.InvoicesService;
using NUnit.Framework;

namespace BMServiceGenerator.Tests
{
    /// <summary>
    /// Note that this is a integration test. It requires a deployed service with namespace Test.Test and the test activity within the project to be deployed.
    /// </summary>
    [TestFixture]
    public class ReferenceTests
    {
        [Test]
        public void ActivityReferenceTest()
        {
            var invoiceActivityId = LogInvoiceActivity();
            
            var activityId = Guid.NewGuid().ToString();

            var srv = new ApprovedInvoicesServiceClient();
            srv.BeginActivity(activityId);
            srv.UpdateActivity(activityId, new ApprovedInvoicesServiceType() { InvoiceId = "1001", Amount = 1, ApprovedBy = "xx", ApprovedDate = DateTime.Now });
            srv.AddActitivityReference(activityId, "Invoices", invoiceActivityId);
            srv.EndActivity(activityId);
        }

        [Test]
        public void DataReferenceTest()
        {
            var activityId = Guid.NewGuid().ToString();

            var srv = new ApprovedInvoicesServiceClient();
            srv.BeginActivity(activityId);
            srv.UpdateActivity(activityId, new ApprovedInvoicesServiceType() { InvoiceId = "1001", Amount = 1, ApprovedBy = "xx", ApprovedDate = DateTime.Now });
            srv.AddReference(activityId, "DocumentUrl", "Approved invoice url", "http://ShowInvoices/Invoice/1001");
            srv.EndActivity(activityId);
        }

        [Test]
        public void LongDataReferenceTest()
        {
            var activityId = Guid.NewGuid().ToString();

            var srv = new ApprovedInvoicesServiceClient();
            srv.BeginActivity(activityId);
            srv.UpdateActivity(activityId, new ApprovedInvoicesServiceType() { InvoiceId = "1001", Amount = 1, ApprovedBy = "xx", ApprovedDate = DateTime.Now });
            srv.AddLongReference(activityId, "Invoice", "Invoice", "http://ShowInvoices/Invoice/1001", CreateInvoiceXmlBody());
            srv.EndActivity(activityId);
        }

        private static string CreateInvoiceXmlBody()
        {
            var xml = new XElement("Invoices",
                    new XElement("Invoice",
                        new XAttribute("InvoiceId", "2"),
                        new XElement("Amount", "200"),
                        new XElement("ApprovedDate", "2011-01-01")
                    )
                );

            return xml.ToString();
        }

        private static string LogInvoiceActivity()
        {
            var srv = new InvoicesServiceClient();
            var activityId = Guid.NewGuid().ToString();
            srv.LogCompleteActivity(activityId, new InvoicesServiceType() { InvoiceId = "1001", Amount = 1, ReceivedDate = DateTime.Now });

            return activityId;
        }

    }
}
